﻿///<reference path = "~/js/jquery-vsdoc.js"/>
/// <reference name="MicrosoftAjax.js"/>
var secTable;
var mainTable;
var PARENT_ID = '';
var jDialog;
$(function () {
    secTable = $('#H8_Tab_v3_secTable');
    mainTable = $('#mainTable');

    jDialog = $('#jDialog').hide();
    jDialog.dialog({
        bgiframe: true,
        autoOpen: false,
        width: 320,
        height: 140,
        modal: true,
        position: 'center',
        stack: true,
        title: '添加子项',
        zIndex: 2010
    });
    FixUI();
    Init();
});

//修正宽带之类的东西
function FixUI() {
    //$('td:eq(3)', secTable).text('现存健康问题');
    $('td:eq(0)', secTable).addClass('sec2_3');
    $('tbody[ck]', mainTable).hide();
    var ck = $('td.sec2_3').attr('ck');
    $('tbody[ck="' + ck + '"]', mainTable).show()
    .find('table[enumkey]').each(function () {
        ExForEnum($(this).attr('enumkey'));
    });
}
function Init() {
    // tab切换
    $('td:not(.end)', secTable).click(function () {
        $('td', secTable).removeClass('sec2_3');
        $(this).addClass('sec2_3');
        $('tbody[ck]', mainTable).hide();
        var ck = $('td.sec2_3').attr('ck');
        $('tbody[ck="' + ck + '"]', mainTable).show()
        .find('table[enumkey]').each(function () {
            ExForEnum($(this).attr('enumkey'));
        });
    });

    $('a', secTable).click(function (i, j, k) {
        var index = $('td', secTable).index($('td.sec2_3'));
        var len = $('td:not(.end)', secTable).size();
        var aindex = $('a', secTable).index($(this));
        if ((index == 0 && aindex == 0) || (index == len - 1 && aindex == 1)) {
            return false;
        }

        if (aindex == 0) {
            index--;
        }
        if (aindex == 1) {
            index++;
        }
        $("td:eq(" + index.toString() + ")", secTable).click();
        return false;
    }).hide();

    $('input.button_w', mainTable).click(function () {
        var text = $(this).prev().val();
        if ($.trim(text) == '') {
            alert('不能添加空值');
            return false;
        }
        PARENT_ID = '';
        var ENUM_KEY = $(this).parent().attr('enumkey');
        var parms = {
            DESCRIPTION: text,
            ActionName: 'Add',
            PARENT_ID: PARENT_ID,
            ENUM_KEY: ENUM_KEY
        };

        $.post(AppPath + '/do/EnumEx.do', parms, function (obj) {
            ExForEnum(ENUM_KEY);
        }, 'json');

    });
}

function ExForEnum(ENUM_KEY) {
    $.post('ExForEnum.aspx', { ENUM_KEY: ENUM_KEY, rd: Math.random() }, function (page) {
        var trs = $('table.table_personal', page).children();
        var exTable = $('table[enumkey="' + ENUM_KEY + '"]');
        exTable.empty().append(trs);

        //删除s
        $('input:odd', trs).click(function () {
            var exid = $(this).parents('tr[exid]').attr('exid');
            if ($('tr[pid="' + exid + '"]').size() == 1) {
                alert("含有子节点不能删除");
                return false;
            }
            if (!confirm('确定删除吗?')) {
                return;
            }
            $.post(AppPath + '/do/EnumEx.do', { ActionName: 'Del', ID: exid }, function (obj) {
                if (obj.ok) {
                    $('tr[exid="' + exid + '"]').remove();
                } else {
                    alert(obj.msg);
                    window.location = window.location;
                }

            }, 'json');
        });

        //添加子项
        $('input:even', trs).click(function () {
            PARENT_ID = $(this).parents('tr[exid]').attr('exid');
            var ENUM_KEY = $(this).parents('table[enumkey]').attr('enumkey');
            jDialog.dialog('open');
            $(':button', jDialog).unbind();

            //确定
            $('input:eq(1)', jDialog).click(function () {
                var text = $(':text', jDialog).val();
                if ($.trim(text) == '') {
                    alert('不能添加空值');
                    return false;
                }
                var parms = {
                    DESCRIPTION: text,
                    ActionName: 'Add',
                    PARENT_ID: PARENT_ID,
                    ENUM_KEY: ENUM_KEY
                };

                $.post(AppPath + '/do/EnumEx.do', parms, function (obj) {
                    jDialog.dialog('close');
                    ExForEnum(ENUM_KEY);
                }, 'json');
            });

            //取消
            $('input:eq(2)', jDialog).click(function () {
                jDialog.dialog('close');
            });
        });

    }, 'html');
}

